package com.infocollect.project.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.infocollect.project.api.entity.TappDeptEntity;
import com.infocollect.project.api.entity.TappOrganEntity;
import com.infocollect.project.api.entity.TappUserEntity;
import com.infocollect.project.api.mapper.TappUserMapper;
import com.infocollect.project.api.service.ITappDeptService;
import com.infocollect.project.api.service.ITappOrganService;
import com.infocollect.project.api.service.ITappUserService;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import com.infocollect.project.api.vo.TappUserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * (TappUser)表服务实现类
 *
 * @author makejava
 * @since 2019-12-25 10:32:31
 */
@Service("tappUserService")
public class TappUserServiceImpl extends ServiceImpl<TappUserMapper, TappUserEntity> implements ITappUserService {

    @Autowired
     private ITappOrganService tappOrganService;

    @Autowired
    private ITappDeptService tappDeptService;

    /**
     * 通过ID查询单条数据
     *
     * @param fid 主键
     * @return 实例对象
     */
    @Override
    public TappUserVO queryById(Long fid) {
        TappUserEntity tappUser= this.baseMapper.selectById(fid);
        TappUserVO tappUserVO=new TappUserVO();
        BeanUtils.copyProperties(tappUser, tappUserVO);
        TappOrganEntity tappOrganEntity= tappOrganService.queryById(tappUserVO.getFdeptid());
        if(tappOrganEntity!=null){
            tappUserVO.setOrganName(tappOrganEntity.getForganname());
        }
        TappDeptEntity tappDeptEntity= tappDeptService.queryById(tappUserVO.getFdeptid());
        if(tappOrganEntity!=null){
            tappUserVO.setDeptName(tappDeptEntity.getFdeptname());
        }
        return tappUserVO;
    }



    /**
     * 新增数据
     *
     * @param tappUser 实例对象
     * @return 实例对象
     */
    @Override
    public TappUserEntity insert(TappUserEntity tappUser) {
        this.baseMapper.insert(tappUser);
        return tappUser;
    }

    /**
     * 修改数据
     *
     * @param tappUser 实例对象
     * @return 实例对象
     */
    @Override
    public TappUserEntity update(TappUserEntity tappUser) {
        this.baseMapper.updateById(tappUser);
        return this.queryById(tappUser.getFid());
    }

    /**
     * 通过主键删除数据
     *
     * @param fid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long fid) {
        return this.baseMapper.deleteById(fid) > 0;
    }
    @Override
    public CurrentUserInfoVO login(String username){
        return this.baseMapper.login(username) ;
    }

    /**
     * 查询多条数据
     * @menu 根据实体对象是否有值 进行查询
     * @return 对象列表
     */
    @Override
    public List<TappUserEntity> queryAll(TappUserEntity user){
        return this.baseMapper.queryAll(user) ;
    }
    /**
     * 根据用户名称和真实名称查询
     * @param workkey
     * @return
     */
    @Override
    public List<TappUserEntity> queryUsernameOrRealaname(String workkey){
        return this.baseMapper.queryUsernameOrRealaname(workkey) ;
    }
}
